<template>
  <el-dialog :visible="visible" title="选择商品" @close="close">
    <!--条件查询-->
    <div class="toolbar">
      <span> 商品编码:</span>
      <el-input class="search_ipt" clearable placeholder="请输入商品编码" size="mini">
      </el-input>
      <span>商品名称:</span>
      <el-input class="search_ipt" clearable placeholder="请输入商品名称" size="mini">
      </el-input>
      <el-button size="mini" style="margin-left: 10px;" type="primary">查询</el-button>
      <el-button size="mini" style="margin-left: 10px;" type="info">重置</el-button>
    </div>

    <!--表格-->
    <el-table :data="tableData" border style="width: 100%" @selection-change="chg">
      <el-table-column align="center" header-align="center" type="selection" width="80"></el-table-column>
      <el-table-column align="center" header-align="center" label="序号" type="index" width="80"></el-table-column>
      <el-table-column header-align="center" label="商品编码" prop="name" width="180"></el-table-column>
      <el-table-column header-align="center" label="商品名称" prop="name"></el-table-column>
      <el-table-column header-align="center" label="规格" prop="model" width="180"></el-table-column>
      <el-table-column align="center" header-align="center" label="单位" prop="unit" width="80"></el-table-column>
    </el-table>
    <!--分页-->
    <div style="text-align: right;margin-top: 20px;display: flex;align-items: center;justify-content: flex-end;">
      <el-pagination :page-size="pageSize" :page-sizes="[10,20,30,40,50,100]" :total="total"
                     background layout="total,sizes,prev, pager, next" style="margin-right: 60px;"
                     @current-change="pageChange"
                     @size-change="sizeChange">
      </el-pagination>
      <el-button size="mini" type="primary" @click="choose">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      pageNum: 1, //默认显示第一页
      pageSize: 10, //每页显示的记录数
      total: 0, //总记录数
      sels: [],
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.query()
  },
  methods: {
    choose() {
      if (this.sels.length === 0) {
        this.$message({
          message: "请选择商品",
          type: "warning"
        });
        return;
      }

      this.$emit("selected", this.sels);
      this.close();
    },
    chg(section) {
      this.sels = section;
    },
    close() {
      this.$emit("update:visible", false);
    },
    query() {
      let params = {
        "pageNum": this.pageNum,
        "pageSize": this.pageSize,
      };
      this.$api.chooseProduct(params)
        .then(res => {
          this.tableData = res.data.list;
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.total = res.data.total;
        })
    },
    //当页数改变时
    pageChange(page) {
      this.pageNum = page;
      this.initData();
    },
    //当每页记录数改变时
    sizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initData();
    }
  }
}
</script>

<style scoped>
.toolbar {
  height: 50px;
  line-height: 50px;
  margin-bottom: 10px;
}

.toolbar span {
  margin-left: 20px;
}

.search_ipt {
  width: 200px;
  margin-left: 5px;
}
</style>
